//
// Created by jxq on 19-8-28.
//
#include "../Acceptor.h"
#include "../EventLoop.h"
#include "../InetAddress.h"
#include "../SocketsOps.h"
#include "../../base/Timestamp.h"
#include <stdio.h>

void newConnection(int sockfd, const muduo::InetAddress& peerAddr)
{
    printf("newConnection(): accepted a new connection from %s\n",
           peerAddr.toHostPort().c_str());
    //::write(sockfd, "How are you?\n", 13);
    ::write(sockfd, (muduo::Timestamp::now().toFormattedString().c_str()), sizeof(muduo::Timestamp::now().toFormattedString().c_str()));
    muduo::sockets::close(sockfd);
}

void newConnection00(int sockfd, const muduo::InetAddress& peerAddr)
{
    printf("newConnection(): accepted a new connection from %s\n",
           peerAddr.toHostPort().c_str());
    ::write(sockfd, "How are you?\n", 13);
    //::write(sockfd, (muduo::Timestamp::now().toFormattedString().c_str()), sizeof(muduo::Timestamp::now().toFormattedString().c_str()));
    muduo::sockets::close(sockfd);
}

int main()
{
    printf("main(): pid = %d\n", getpid());

    muduo::InetAddress listenAddr00(9982);
    muduo::InetAddress listenAddr(9981);
    muduo::EventLoop loop;

    muduo::Acceptor acceptor(&loop, listenAddr);
    acceptor.setNewConnectionCallback(newConnection);
    acceptor.listen();

    muduo::Acceptor acceptor00(&loop, listenAddr00);
    acceptor00.setNewConnectionCallback(newConnection00);
    acceptor00.listen();

    loop.loop();
}